package org.eclipse.football;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SortTeamServlet extends HttpServlet implements Servlet {
	// Override doPost function
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// Get sorting type integer submitted by the form
		int sort_type = Integer.parseInt(request.getParameter("type"));
		
		// Obtain existing league
		League league = (League) request.getSession(true).getAttribute("league");
		
		// Make sure league is non null
		if(league != null){
			// If so sort the league by given type
			league.sort(sort_type);
		}
		
		// Write the state of the league out to a file
		league.writeState("league.txt");
		
		// Set league in the session
		request.getSession().setAttribute("leage", league);
		
		// Make a null team and set it in the session
		Team team = null;
		request.getSession().setAttribute("team", team);

		// Set all 3 player types to null in the session
		request.getSession().setAttribute("off", null);
		request.getSession().setAttribute("def", null);
		request.getSession().setAttribute("kicker", null);
		
		// Forward control back to stats.jsp
		getServletContext().getRequestDispatcher("/LeagueSummary.jsp").forward(request, response);
		
		// TODO Auto-generated method stub
		//super.doPost(request, response);
	}

}

